import request from '@/utils/request';
import { requestWrapper, randomDate, randomInt, randomPick, paginate } from '@/utils/mock';

// 获取角色列表
export function getRoleList(params) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/system/role/list',
      method: 'get',
      params
    }),
    // 模拟数据
    () => {
      const statusOptions = ['0', '1']; // 0正常 1停用
      const roles = Array.from({ length: 10 }, (_, index) => ({
        roleId: index + 1,
        roleName: `角色${index + 1}`,
        roleKey: `role_${index + 1}`,
        roleSort: index + 1,
        status: randomPick(statusOptions),
        createTime: randomDate(-30)
      }));

      // 处理搜索条件
      let filteredRoles = [...roles];

      if (params) {
        if (params.roleName) {
          filteredRoles = filteredRoles.filter(item => 
            item.roleName.includes(params.roleName)
          );
        }
        
        if (params.status !== undefined && params.status !== '') {
          filteredRoles = filteredRoles.filter(item => 
            item.status === params.status
          );
        }
      }

      // 处理分页
      const pageNum = params?.pageNum ? parseInt(params.pageNum) : 1;
      const pageSize = params?.pageSize ? parseInt(params.pageSize) : 10;
      const startIndex = (pageNum - 1) * pageSize;
      const endIndex = pageNum * pageSize;
      const pageData = filteredRoles.slice(startIndex, endIndex);

      return {
        list: pageData,
        total: filteredRoles.length
      };
    }
  );
}

// 获取角色详情
export function getRoleDetail(roleId) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: `/system/role/${roleId}`,
      method: 'get'
    }),
    // 模拟数据
    {
      roleId,
      roleName: `角色${roleId}`,
      roleKey: `role_${roleId}`,
      roleSort: roleId,
      status: '0',
      remark: `所有权限${roleId}`,
      menuIds: Array.from({ length: randomInt(3, 10) }, () => randomInt(1, 50))
    }
  );
}

// 新增角色
export function addRole(data) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/system/role',
      method: 'post',
      data
    }),
    // 模拟数据
    null
  );
}

// 修改角色
export function updateRole(data) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/system/role',
      method: 'put',
      data
    }),
    // 模拟数据
    null
  );
}

// 删除角色
export function deleteRole(roleIds) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: `/system/role/${roleIds}`,
      method: 'delete'
    }),
    // 模拟数据
    null
  );
}

// 分配角色权限
export function assignRolePermissions(data) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/system/role/auth',
      method: 'put',
      data
    }),
    // 模拟数据
    null
  );
}

// 更新角色状态
export function updateRoleStatus(data) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/system/role/status',
      method: 'put',
      data
    }),
    // 模拟数据
    null
  );
}

// 获取角色下拉选项
export function getRoleOptions() {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/system/role/options',
      method: 'get'
    }),
    // 模拟数据
    Array.from({ length: 5 }, (_, index) => ({
      value: index + 1,
      label: `角色${index + 1}`
    }))
  );
} 